#include "display/game/CharacterGraphic.hpp"

#include "util/Text.h"

CharacterGraphic::CharacterGraphic(Character* character) : Displayable(0), m_character(character)
{
    m_image = new sf::Image;
    if (!m_image->LoadFromFile("sprites/character.png"))
        Debug::printTrace(Debug::critical, "display/game/CharacterGraphique.constructor", "Unable to load the picture");

    m_sprite = new sf::Sprite();

    m_sprite->SetImage(*m_image);
    if(character->GetName() != GameInfo::m_PlayerName)
    	m_sprite->SetSubRect(sf::IntRect(32*9,32*0,32*10-2,32*1-2));
    else
    	m_sprite->SetSubRect(sf::IntRect(32*9,32*4,32*10-2,32*5-2));
}

CharacterGraphic::~CharacterGraphic()
{
    delete m_sprite;
    delete m_image;
}
/**
 * draw the caracter if he is alive
 */
void CharacterGraphic::draw(sf::RenderWindow* window)
{
	//Debug::printTrace(Debug::critical, "display/game/CharacterGraphique.draw",  Text::float2string(m_character->Getposition().x));
	//Debug::printTrace(Debug::critical, "display/game/CharacterGraphique.draw",  Text::float2string(m_character->Getposition().y));
	if(this->m_character->GetState() == Character::ALIVE)
	{
		m_sprite->SetPosition(m_character->Getposition());

		window->Draw(*m_sprite);

		this->drawLife(window);
	}
}
/**
 * Not implemented
 */
void CharacterGraphic::DoEvent(sf::Event e)
{
	//nothing
}
/**
 * Not implemented
 */
void CharacterGraphic::DoRealTimeEvent(const sf::Input& p_event)
{
	//nothing
}
/**
 * draw the caracter lifeBar
 */
void CharacterGraphic::drawLife(sf::RenderWindow* window)
{
	sf::Vector2f playerPosition = m_character->Getposition();

	sf::Shape lifeBar = sf::Shape::Rectangle(
			playerPosition.x,
			playerPosition.y - 5,
			playerPosition.x + (m_character->GetLife()) * DefaultValue::PLAYER_SIZE / DefaultValue::PLAYER_LIFE,
			playerPosition.y - 2, sf::Color::Red);

    window->Draw(lifeBar);
}
